package com.zjdiepu.www.biz.dz;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dz.DzMortgageSubMapper;
import com.zjdiepu.www.ibiz.dz.IDzMortgageSubService;
import com.zjdiepu.www.model.dz.dto.DzMortgageSub;
import com.zjdiepu.www.model.dz.ext.DzMortgageSubExt;

/**
 * 车辆抵押确认_子表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月11日 16时17分<br/>
 * @since 1.0<br/>
 */
@Service
public class DzMortgageSubServiceImpl extends BaseService implements IDzMortgageSubService {
	public static final Logger logger = LoggerFactory.getLogger(DzMortgageSubServiceImpl.class);

	@Autowired
	private DzMortgageSubMapper dzMortgageSubMapper;
	
	@Override
	public void saveUpdateAndDelete(DzMortgageSub dzMortgageSub, OperateType operType) {
		switch (operType) {
		case add:
			dzMortgageSub.initInsertData(dzMortgageSub.getModifier(), dzMortgageSub.getModifierName());
			dzMortgageSubMapper.insert(dzMortgageSub);
			break;
		case edit:
			dzMortgageSub.initUpdateData(dzMortgageSub.getModifier(), dzMortgageSub.getModifierName());
			// 影响的记录数
			int updateCount = dzMortgageSubMapper.updateByPrimaryKeySelective(dzMortgageSub);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dzMortgageSub.initDeleteData(dzMortgageSub.getModifier(), dzMortgageSub.getModifierName());
			// 影响的记录数
			int deleteCount = dzMortgageSubMapper.updateByPrimaryKeySelective(dzMortgageSub);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DzMortgageSub> selectByExt(DzMortgageSubExt dzMortgageSubExt) {
		return dzMortgageSubMapper.selectByExt(dzMortgageSubExt);
	}
	
	@Override
	public int countByExt(DzMortgageSubExt dzMortgageSubExt){
		return dzMortgageSubMapper.countByExt(dzMortgageSubExt);
	}
	
	@Override
	public DzMortgageSub selectById(String id) {
		return dzMortgageSubMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dzMortgageSubMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DzMortgageSub dzMortgageSub) {
		return dzMortgageSubMapper.countRefined(dzMortgageSub);
	}

	@Override
	public DzMortgageSub selectRefined(DzMortgageSub dzMortgageSub) {
		return dzMortgageSubMapper.selectRefined(dzMortgageSub);
	}
	
	@Override
	public int deletedRefined(DzMortgageSub dzMortgageSub) {
		return dzMortgageSubMapper.deletedRefined(dzMortgageSub);
	}

	@Override
	public boolean repeat(DzMortgageSub dzMortgageSub) {
		int count = countRefined(dzMortgageSub);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dzMortgageSub.getId())) {
			return true;
		}
		DzMortgageSub dbItem = selectRefined(dzMortgageSub);
		if(null == dbItem) {
			return false;
		}
		return !dzMortgageSub.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dzMortgageSubMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DzMortgageSub> dzMortgageSubs) {
		return dzMortgageSubMapper.insertBatch(dzMortgageSubs);
	}
}
